package com.linwen.excise.leetcode;

/**
 * @author : linwen
 * @date : 11:10 下午 2020/10/27
 */
public class _283移动零 {
    public static void main(String[] args) {
        int[] input = new int[]{0,1,0,3,12};
        int[] result = moveZeros(input);
        for(int i=0;i<result.length;i++){
            System.out.print(result[i]+" ");
        }
    }

    private static int[] moveZeros(int[] input) {
        //index表示非0的元素下标，将所有非0元素按顺序排序
        int index=0;
        for (int i = 0; i <input.length ; i++) {
            if(input[i]!=0){
                input[index]=input[i];
                index++;
            }
        }
        for (int i = index; i <input.length ; i++) {
            input[i]=0;
        }
        return input;
    }
}
